﻿<cfsilent>
	<cfscript>

		event.setArg("pageTitle", "教学运行管理 - 补考安排 - 补考全自动编排");

		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");

		termId = event.getArg("TermID");

		academicYear = event.getArg("AcademicYear");
		academicTerm = event.getArg("AcademicTerm");
		
		currentTab = event.getArg("TabID", "labArrange");
		
		passArgs = structNew();
		
		targetArgs = structNew();
		structInsert(targetArgs, "TID", termId);
		
		sql = "SELECT 
					a.task_exrstid, 
					b.course_name, 
					c.campus_name, 
					a.examer_sum, 
					d.institute_short  
				FROM t_resittask a  
					 INNER JOIN t_course b ON b.cid = a.cid 
					 INNER JOIN t_campus c ON c.campus_id = a.campus_id 
					 INNER JOIN t_institute d ON d.institute_id = b.institute_id 
				WHERE 
					a.term_id = :termId 
					AND 
					a.task_prog = '0' 
				ORDER BY a.examer_sum DESC ";
		
		queryObj = new Query( datasource=application.dnsSlave ) ;
   		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		
		rs_task= queryObj.execute( sql=sql ).getResult();
		
		
		sql = "SELECT 
					a.task_exrstid, 
					b.course_name, 
					c.campus_name, 
					a.examer_sum, 
					d.institute_short  
				FROM t_resittask a  
					 INNER JOIN t_course b ON b.cid = a.cid 
					 INNER JOIN t_campus c ON c.campus_id = a.campus_id 
					 INNER JOIN t_institute d ON d.institute_id = b.institute_id 
				WHERE 
					a.term_id = :termId 
					AND 
					a.task_prog = '1' 
				ORDER BY a.examer_sum DESC ";
		
		queryObj = new Query( datasource=application.dnsSlave ) ;
   		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		
		rs_taskTimeOnly= queryObj.execute( sql=sql ).getResult();

		examTaskList = "";
		
		for (   a=1; a LTE rs_task.recordCount; a++   ) {
			examTaskList = listAppend(examTaskList, "['TP','" & rs_task["task_exrstid"][a] & "','" & rs_task["institute_short"][a] & "-" & rs_task["course_name"][a] & "','" & rs_task["campus_name"][a] & "'," & rs_task["examer_sum"][a] & "]");
		}
		
		for (   a=1; a LTE rs_taskTimeOnly.recordCount; a++    ) {
			examTaskList = listAppend(examTaskList, "['OP','" & rs_taskTimeOnly["task_exrstid"][a] & "','" & rs_taskTimeOnly["institute_short"][a] & "-" & rs_taskTimeOnly["course_name"][a] & "','" & rs_taskTimeOnly["campus_name"][a] & "'," & rs_taskTimeOnly["examer_sum"][a] & "]");
		}
	
	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('examResitArrange')#</cfoutput>"><i class="mrs img btnList"></i><span class="uiButtonText">补考编排</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 config"></i>补考任务自动编排
				</h2>
			</div>
		</div>
	</div>
</div>

<div class="UIElement">
	<ul class="senateFirstPage">
		<li>
			<div class="clearfix">
				
				<div class="UItab">
					<div class="tabLables">
						<span <cfif currentTab eq "labArrange">class="active"</cfif> id="labArrange" tabTarget="Arrange">全自动编排</span>
					</div>
				</div>
				<script language="javascript" type="text/javascript">
					//<![CDATA[
					<!--
					$(document).ready(function() {
						
						/* init Tab */
						$("div.tabContent").hide();
						$("div#" + $("div.tabLables span.active").attr("tabTarget")).show();
						
						$("div.tabLables > span").click(function(){
							var $targetID = $(this).attr("tabTarget");
							
							$("div.tabLables span").removeClass("active");
							$(this).addClass("active");
							
							$("div.tabContent").not('[id="' + $targetID + '"]').hide();
							$("div.tabContent#" + $targetID).show();
						});
						
					});
					// -->
					//]]>
				</script>
				
				<div id="Arrange" class="tabContent">
					
					<div class="noticeBlock">
						<h3>自动编排课程补考时间及场地</h3>
						<p>
							<span class="img icon16x16 message"></span>
							<em><cfoutput>#academicYear#</cfoutput>-<cfoutput>#academicTerm#</cfoutput></em>学期, 
							有<em><cfoutput>#rs_task.recordCount#</cfoutput></em>场次考试需要安排时间和考试场地, 
							有<em><cfoutput>#rs_taskTimeOnly.recordCount#</cfoutput></em>场次考试需编排考试场地
						</p>
					</div>
					
					<div id="out"></div>
					
					<form id="formAuto" class="formWrapper" method="post" action="<cfoutput>#buildURL('examResitArrangeFullAutomatic')#</cfoutput>">
						
						<div class="label">
							<span class="req">*</span><b>注意</b>
							<div class="secondRow">
								自动编排过程, 会占用大量的计算资源, 请选择在系统负载较低的时间段进行. 在自动编排期间, 请勿手动编排本学期任课课程的补考, 以免造成考试时间冲突.
								<br/>
								<br/>
								在此期间, 请保持客户端网络畅通, 避免网络延迟造成的安排停顿.
							</div>
						</div>
						
						<hr/>
						
						<div class="lable">
							<input id="triggerAuto" class="button" type="button" value="自动编排" />
						</div>
						
					</form>
					
					<div id="statLogger" class="noticeBlock hidden_elem">
						<h3>正在自动进行考试安排</h3>
						
						<div class="UIProgressbar">
							<span class="progressBar" id="pb1">0%</span>
							<p id="pbmsg">处理队列初始化</p>
						</div>
						
						<div id="logger"></div>
						<hr/>
					</div>
					<div id="outer"></div>

					
					<script language="javascript" type="text/javascript">
						//<![CDATA[
						<!--
						$(document).ready(function() {
							
							$("#triggerAuto").click(function() {
							
								$("#formAuto").addClass("hidden_elem");
								
								$("#statLogger").removeClass("hidden_elem");
								
								/* init Progress */
								$("#pb1").progressBar({
														boxImage : '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbar.gif',
														barImage : {
															0:  '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_red.gif',
															30: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_orange.gif',
															70: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_green.gif'
														}
														});
								
								/* begin operation */
								progress(0);

							});
							
							
							var examTask = [<cfoutput>#examTaskList#</cfoutput>];
							
							/* arrange each exam task */
							function progress(current) {
								
								$("#pbmsg").html("正在自动编排: " + examTask[current][2] + ", 将 " + examTask[current][4] + " 人编排至 " + examTask[current][3] + " 考试."  );
								
								switch ( examTask[current][0] ) {
									
									case "TP":
										var targetEvent = "<cfoutput>#buildURL('apiExamResitTaskAvailableTimes')#</cfoutput>";
										break;
									
									case "OP":
										var targetEvent = "<cfoutput>#buildURL('apiExamResitTaskSetPlace')#</cfoutput>";
										break;
								}
								
								$.post(
										targetEvent, 
										{
											'exid' : examTask[current][1],
											'seed': (new Date()).getTime()
										}, 
										function(data) {

											result = $.parseJSON(data);
											
											if (result.success) {
												
												/**/
												var logger = $("<ul></ul>");
													
												for ( var e=0; e < result.Progress.length; e++ ) {
													$("<li></li>").html(result.Progress[e]).appendTo(logger);
												}

												$("#logger").html(logger);
												
												var tabler = $("<table></table>").addClass("UIEditable");
												var thead = $("<thead><tr><td width='20'></td><td>考场</td><td>容量</td><td>考试人数</td><td>座次编号</td></tr></thead>")
												var tbody = $("<tbody></tbody>");
													
												var i = 0;
												for ( var e=0; e < result.ClassRoom.length; e++ ) {
													i++;
													var tr = $("<tr></tr>").addClass("editRows");
													
													$("<td></td>").addClass("index").html(i).appendTo(tr);
													$("<td></td>").html(result.ClassRoom[e][0]).appendTo(tr);
													$("<td><span></span></td>").addClass("numeric").html(result.ClassRoom[e][1]).appendTo(tr);
													$("<td><span></span></td>").addClass("numeric").html(result.ClassRoom[e][2]).appendTo(tr);
													$("<td><span></span></td>").addClass("numeric").html(result.ClassRoom[e][3] + " - " + result.ClassRoom[e][4]).appendTo(tr);
														
													tr.appendTo(tbody);	
												}
													
												thead.appendTo(tabler);
												tbody.appendTo(tabler);
													
												$("#outer").html(tabler);
												
											}
											else {
												$("#outer").html(result.reason);
											}
											
											/* fresh progress bar */
											var percentage = Math.floor(100 * parseInt(current + 1) / parseInt(examTask.length));

											$("#pb1").progressBar(
																	percentage,
																	{
																		boxImage : '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbar.gif',
																		barImage : {
																			0:  '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_red.gif',
																			30: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_orange.gif',
																			70: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_green.gif'
																	},
																	callback: function(running){
																	
																		if (running.running_value == running.value) {
																	
																			current++;
																			
																			if ( current < examTask.length) {
																				
																				/* 延迟50毫秒跳转 */
																				$('body').oneTime('5cs',function(){
																					progress(current);
																				});
																			
																			}
																			else {
																				$("#pbmsg").text("完成");
																				
																				/* 延迟2秒跳转 */
																				$('body').oneTime('2s',function(){
																					window.location = "<cfoutput>#buildURL('examResitArrange')#</cfoutput>";
																				});
																				
																			}
																		}
																	}
																});
											
											
																							
												

											
										});
										
							}

						});
						// -->
						//]]>				
					</script>
					
				</div>
				
			</div>
		</li>
	</ul>
</div>




